//
//  ShelvesAlreadlyViewController.m
//  PigMarket
//
//  Created by 张敬 on 2019/8/15.
//  Copyright © 2019年 pigmarket.oc. All rights reserved.
//

#import "ShelvesAlreadlyViewController.h"
#import "SeparatePlanCell.h"


@interface ShelvesAlreadlyViewController ()

@property (nonatomic, strong) ShopSeparateModel *model;


@end

@implementation ShelvesAlreadlyViewController

- (void)setupUI{
    
    self.baseNavView.hidden = YES;
}


- (void)setupLayout{
    
    {
        self.reloadBtn.mj_y -= 100;
        self.reloadLab.mj_y -= 100;
    }
}


#pragma mark - networking methods

- (void)initData{
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = page;
    param.limit = @10;
    [self requestMyShopSaleByParam:param];
}


//我得店铺销售 Yes
- (void)requestMyShopSaleByParam:(ShopMineSaleParam *)param{

    @weakify(self);
    [[[MeatShopApi sharedInstance] getMyShopSaleByParams:param] subscribeNext:^(ShopSeparateModel * _Nullable model) {
        
        @strongify(self);
        
        self.model = model;
        
        if (self.isRefresh) {
            [self endRefresh];
        }
        
        if ([self.model.page.totalCount intValue] < [self.model.page.pageSize intValue] ) {
            [self endLoadMoreMoData];
        }else if ([self.model.page.totalCount intValue] == [self.model.page.pageSize intValue]){
            
            if ([self.model.page.currPage intValue] >= [self.model.page.totalCount intValue]) {
                [self endLoadMoreMoData];
            }else{
                [self endLoadMore];
            }
        }
        
        if (self.model.page.list.count >0) {
            self.tableView.hidden = NO;
            [self.tableView reloadData];
        }else{
            [self loadNoData];
        }
        
    }error:^(NSError * _Nullable error) {
        @strongify(self);
        [self loadErrorData];
    }];
}


//下拉刷新
- (void)refresh
{
    [super refresh];
    [self resetData];
    [self initData];
}

//上拉加载更多
- (void)loadMore
{
    [super loadMore];
    
    NSNumber *page = [NSNumber numberWithInteger:self.page];
    ShopMineSaleParam *param = [ShopMineSaleParam new];
    param.shopId = @0;
    param.memberId = kMemberId;
    param.pigId = @1;
    param.page = page;
    param.limit = @10;
    [self requestMyShopSaleByParam:param];
}


#pragma mark - tableview.delegate methods

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"点击tableView");
    
}


#pragma mark - tableview.datascource methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.model.page.list.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}


- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    return  [UIView new];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SeparatePlanCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SeparatePigCell"];
    
    if (self.model.page.list.count >0) {
        ShopSeparateList *item = self.model.page.list[indexPath.section];
        cell.item = item;
    }

    return cell;
}

#pragma mark - setter methods

#pragma mark - getter methods

- (void)registerTableViewCell
{
    self.tableView.rowHeight = 118;
    self.tableView.height = KScreenHeight - TJ_NavBarHeight -50;
    self.tableView.mj_y = 0;
    self.tableView.separatorColor = mIconLineColor;
    [self.tableView registerClass:[SeparatePlanCell class] forCellReuseIdentifier:@"SeparatePigCell"];
}


- (ShopSeparateModel *)model{
    
    if (!_model) {
        _model = [[ShopSeparateModel alloc] init];
    }
    return _model;
}


//#pragma mark override methods
//
//- (void)loadErrorData
//{
//    [super loadErrorData];
//    self.footer.hidden = YES;
//}
//
//- (void)loadNoData
//{
//    [super loadNoData];
//    self.footer.hidden = YES;
//}
//
//
//- (void)endLoadMore{
//    [super endLoadMore];
//    self.footer.hidden = NO;
//}
//
//- (void)endRefresh{
//    [super endRefresh];
//    self.footer.hidden = NO;
//}
//
//- (void)endLoadMoreMoData
//{
//    [super endLoadMoreMoData];
//    self.footer.hidden = NO;
//}
//




@end
